package com.demboos.galaxy.radio.widget;

import android.content.Context;
import android.content.SharedPreferences;

public class WidgetConfigDatabase {

	private static final String PREFS_NAME = "com.demboos.galaxy.radio.widget";
	private static final String PREF_LAYOUTKEY_FORMAT = "widget_%d_layout";
	private static final String PREF_LAST_PLAYED_FREQUENCY = "last_played_frequency";
	private static final String PREF_FM_RADIO_REGION = "region";
	
	public static final String REGION_EUROPE = "europe";
	public static final String REGION_USA = "usa";
	public static final String REGION_JAPAN = "japan";

	static synchronized void setLayoutKey(Context context, int appWidgetId,
			int layoutId) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putInt(layoutKey(appWidgetId), layoutId);
		prefs.commit();
	}

	static synchronized int getLayoutKey(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		return prefs.getInt(layoutKey(appWidgetId), R.layout.eclair_radio_appwidget);
	}

	static synchronized void delLayoutKey(Context context, int appWidgetId) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.remove(layoutKey(appWidgetId));
		prefs.commit();
	}

	static String layoutKey(int appWidgetId) {
		String out = String.format(PREF_LAYOUTKEY_FORMAT, appWidgetId);

		return out;
	}
	
	static synchronized void setLastPlayedFrequency(Context context, long frequency) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putLong(PREF_LAST_PLAYED_FREQUENCY, frequency);
		prefs.commit();
	}
	
	static synchronized long getLastPlayedFrequency(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		return prefs.getLong(PREF_LAST_PLAYED_FREQUENCY, 0l);
	}
	
	static synchronized void setRadioRegionPref(Context context, String region) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putString(PREF_FM_RADIO_REGION, region);
		prefs.commit();
	}
	
	static synchronized boolean hasRadioRegionPref(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		return prefs.contains(PREF_FM_RADIO_REGION);
	}
	
	static synchronized String getRadioRegionPref(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		return prefs.getString(PREF_FM_RADIO_REGION, REGION_EUROPE);
	}
}
